package com.cloud.basic.webservice.soapClient.SoapAcess;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.cloud.basic.webservice.soapClient.resolver.SoapResolver;

/**
 * soap接口调用器
 * @author eguid
 *
 */
public class SoapAccess implements CommonAccess {

	@Override
	public String commonAccess(String xml, String urlString, String soapAction, String contextType) throws IOException {
		byte[] buf = xml.getBytes(UTF8);
		byte[] datas = invoke(buf, String.valueOf(buf.length), urlString, soapAction, contextType, POST, false, 3000);
		String data=new String(datas, UTF8);
		System.err.println(data);
		return SoapResolver.getSoapResult(data, soapAction);
	}

	@Override
	public String commonAccess(byte[] xml, String urlString, String soapAction, String contextType) throws IOException {
		byte[] datas = invoke(xml, String.valueOf(xml.length), urlString, soapAction, contextType, POST, false, 3000);
		String data=new String(datas, UTF8);
		System.err.println(data);
		return SoapResolver.getSoapResult(data, soapAction);
	}

	@Override
	public byte[] invoke(byte[] xml, String xmlLength, String urlString, String soapAction, String contextType,String reqMethod, boolean useCache, int timeout) throws IOException {
		URL url = new URL(urlString);
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
		httpConn.setRequestProperty("Content-Length", xmlLength);
		httpConn.setRequestProperty("Content-Type", contextType);
		httpConn.setRequestProperty("soapActionString", soapAction);
		httpConn.setRequestMethod(reqMethod);
		httpConn.setUseCaches(useCache);
		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);
		httpConn.setConnectTimeout(timeout);// 超时时间
		OutputStream out;
		out = httpConn.getOutputStream();
		out.write(xml);
		out.close();
		return readInputStream(httpConn.getInputStream());
	}

	@Override
	public byte[] readInputStream(InputStream inStream) {
		ByteArrayOutputStream outStream = null;
		try {
			outStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			byte[] data = null;
			int len = 0;
			while ((len = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, len);
			}
			data = outStream.toByteArray();// 网页的二进制数据
			return data;
		}catch (IOException e) {
			System.err.println(ERR_MSG);
			e.printStackTrace();
		} finally {
			try {
				if (outStream != null) {
					outStream.close();
				}
				if (inStream != null) {
					inStream.close();
				}
			} catch (IOException e) {
				System.err.println(ERR_MSG);
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public String readInputStream(InputStream inStream, String charset) {
		ByteArrayOutputStream outStream = null;
		try {
			outStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			String data = null;
			int len = 0;
			while ((len = inStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, len);
			}
			data = charset == null ? outStream.toString() : outStream.toString(UTF8);
			return data;
		} catch (IOException e) {
			System.err.println(ERR_MSG);
			e.printStackTrace();
		} finally {
			try {
				if (outStream != null) {
					outStream.close();
				}
				if (inStream != null) {
					inStream.close();
				}
			} catch (IOException e) {
				System.err.println(ERR_MSG);
				e.printStackTrace();
			}
		}
		return null;
	}

}
